<?php

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\OrderProducts;
use App\Repository\Models\Orders;
use App\Repository\Repositories\Interfaces\AddressRepository;
use App\Repository\Repositories\Interfaces\CartsRepository;
use App\Repository\Repositories\Interfaces\CompanyRepository;
use App\Repository\Repositories\Interfaces\InvoicesRepository;
use App\Repository\Repositories\Interfaces\OrderExpressRepository;
use App\Repository\Repositories\Interfaces\OrderProductCommentRepository;
use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use App\Repository\Repositories\Interfaces\ShopRepository;
use App\Repository\Repositories\Interfaces\UserInvoiceRepository;
use App\Services\Api\KuaiDi100;
use Illuminate\Http\Request;

/**
 * 商品控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ProductSkusController extends BaseController
{
    /**
     * @var ProductSkusRepository
     */
    protected $product_skus_repo;

    public function __construct(ProductSkusRepository $product_skus_repo)
    {
        parent::__construct();
        $this->shop_id = array_get($this->user, 'shop_id');
        $this->product_skus_repo = $product_skus_repo;;
    }

    /**
     * 购物车下单商铺-商品列表
     * @param Request $request
     * @param ShopRepository $shop_repo
     * @return \Illuminate\Support\Collection
     */
    public function index(Request $request, ShopRepository $shop_repo){
        $this->validate($request, [
            'sku_num' => 'required|array', //例如：[223=>2,322=>1]，key为sku_id，value为购买数量
        ]);
        $data = $request->all(['sku_num']);


        //一次获取所有产品
        $sku_ids = array_keys($data['sku_num']);
        $skus = $this->product_skus_repo->with(['product' => function ($sql) {
            $sql->select(['id', 'shop_id', 'name', 'model', 'brand_id', 'cover_image', 'status', 'is_for_sale']);
        }])->findWhereIn('id', $sku_ids, ['id', 'product_id', 'sale_price', 'attributes', 'stock', 'freight']);

        //根据店铺分组
        $shop_ids = [];
        $shop_skus = [];
        foreach ($skus['data'] as $sku) {
            $shop_ids[] = $sku['product']['shop_id'];
            //存在自己店铺产品
            if($this->shop_id==$sku['product']['shop_id']){
                return $this->error(ErrorConst::PRODUCT_USER_MSG, ErrorConst::PRODUCT_USER);
            }
            //校验库存是否充足
            if($data['sku_num'][$sku['id']] > $sku['stock']){
                return $this->error(ErrorConst::PRODUCT_LOW_STOCKS_MSG, ErrorConst::PRODUCT_LOW_STOCKS);
            }
            $sku['buy_num'] = $data['sku_num'][$sku['id']];
            $shop_skus[$sku['product']['shop_id']][] = $sku;
        }
        $shops = $shop_repo->findWhereIn('sp_id', $shop_ids, ['sp_id', 'sp_name']);

        array_walk($shops['data'], function (&$value, $key) use ($shop_skus) {
            $value['skus'] = $shop_skus[$value['sp_id']];
        });

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shops['data']);
    }

    /**
     * 直接下单商铺-商品列表
     * @param Request $request
     * @param ShopRepository $shop_repo
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function show(Request $request, ShopRepository $shop_repo, $id){
        $this->validate($request, [
            'sku_num' => 'required|integer', //例如：[223=>2,322=>1]，key为sku_id，value为购买数量
        ]);
        $sku_num = $request->get('sku_num');
        $sku = $this->product_skus_repo->with(['product'])->find($id);
        //存在自己店铺产品
        if($this->shop_id==$sku['data']['product']['shop_id']){
            return $this->error(ErrorConst::PRODUCT_USER_MSG, ErrorConst::PRODUCT_USER);
        }
        if($sku_num > $sku['data']['stock']){
            return $this->error(ErrorConst::PRODUCT_LOW_STOCKS_MSG, ErrorConst::PRODUCT_LOW_STOCKS);
        }
        $sku['data']['buy_num'] = $sku_num;

//        $shop = $shop_repo->find(array_get($sku['data'], 'product.shop_id'), ['id', 'company_name']);
        $shop = $shop_repo->find(array_get($sku['data'], 'product.shop_id'), ['sp_id', 'sp_name']);

        $shop['data']['skus'][] = $sku['data'];
        $shops[] = $shop['data'];

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shops);
    }
}
